package com.example.community;

import org.springframework.boot.test.context.SpringBootTest;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.locks.ReentrantLock;

@SpringBootTest
public class testQene {
    public static void main(String[] args) {
        BlockingQueue quene = new ArrayBlockingQueue<>(10);
        new Thread(new producer(quene),"生产者1").start();
        new Thread(new producer(quene),"生产者2").start();
        new Thread(new Comsumer(quene),"消费者1").start();
        new Thread(new Comsumer(quene),"消费者2").start();
    }

}
class Comsumer implements Runnable{

    private BlockingQueue quene;

    public Comsumer(BlockingQueue quene) {
        this.quene = quene;
    }

    @Override
    public void run() {
        try{
            while(true){
                Thread.sleep(50);
                quene.take();
                System.out.println(Thread.currentThread().getName()+"消费");
            }

        }catch (Exception e){
            e.getStackTrace();
        }
    }
}
class producer implements Runnable{

    private BlockingQueue quene;

    public producer(BlockingQueue<Integer> quene) {
        this.quene = quene;
    }

    @Override
    public void run() {
        try{
            for (int i = 0; i < 100; i++){
                Thread.sleep(20);
                quene.put(i);
                System.out.println(Thread.currentThread().getName()+"生产");
                System.out.println(quene.size());
            }

        }catch (Exception e){
            e.getStackTrace();
        }
    }
}